
function findFrame(name, inputFrm){
  		if(inputFrm == null) {inputFrm = top;}
  		if(inputFrm.frames.length > 0)
  		{
	  		var queryFrm = inputFrm.frames[name];
	  		if (queryFrm == null)
	  		{
	  			for(var i = 0, len = inputFrm.frames.length; i< len; i++)
	  			{
	  				if(inputFrm.frames[i] == null) continue;
	  				var inFrm = findFrame(name, inputFrm.frames[i]);
	  				if( inFrm != null){return inFrm;}
	  			}
	  		}else{return queryFrm;}
  		}
}

/*
   * 弹出操作成功信息
   * @param alertInfo 成功信息
*/
function SuccessAlert(alertInfo){
    var url = encodeURI(encodeURI("commons/SuccessAlert.jsp?alertInfo="+alertInfo));
    if(this.windowOpen){  //一定要加上this
        windowOpen(url, 'SuccessAlert', "成功", 190, 400, true, 'SuccessAlertIf');
    }else{
        top.windowOpen(url, 'SuccessAlert', "成功", 190, 400, true, 'SuccessAlertIf');
    }
}

function Alert(alertInfo){
    var url = encodeURI(encodeURI("commons/alert.jsp?alertInfo="+alertInfo));
    if(this.windowOpen){  //一定要加上this
        windowOpen(url, 'alarmAlert', "警告", 190, 400, true, 'alarmAlertIf');
    }else{
        top.windowOpen(url, 'alarmAlert', "警告", 190, 400, true, 'alarmAlertIf');
    }
}

/*
   * 弹出操作失败信息
   * @param alertInfo 失败信息
*/
function FailureAlert(alertInfo){
    var url = encodeURI(encodeURI("commons/FailureAlert.jsp?alertInfo="+alertInfo));
    if(this.windowOpen){  //一定要加上this
       windowOpen(url, 'FailureAlert', "失败", 190, 400, true, 'FailureAlertIf');
    }else{
       top.windowOpen(url, 'FailureAlert', "失败", 190, 400, true, 'FailureAlertIf');
    }
}

/*
   * 弹出校验错误信息
   * @param alertInfo 错误信息
*/
function ErrorAlert(alertInfo){
    var url = encodeURI(encodeURI("commons/ErrorAlert.jsp?alertInfo="+alertInfo));
    if(this.windowOpen){  //一定要加上this
       windowOpen(url, 'ErrorAlert', "错误", 190, 400, true, 'ErrorAlertIf');
    }else{
       top.windowOpen(url, 'ErrorAlert', "错误", 190, 400, true, 'ErrorAlertIf');
    }
}

/*
   * 弹出警告窗口
   * @param alarmInfo          警告信息
   * @param parentFrameId      父frame的Id
   * @param callBackMethodName 回调函数名
*/
function AlarmConfirm(alarmInfo,parentFrameId,callBackMethodName){

    var url = encodeURI(encodeURI("commons/AlarmConfirm.jsp?alarmInfo="+alarmInfo+"&parentFrameId="+parentFrameId+"&callBackMethodName="+callBackMethodName));
    if(this.windowOpen){  //一定要加上this
       windowOpen(url, 'AlarmConfirm', "警告", 190, 400, true, 'AlarmConfirmIf');
    }else{
       top.windowOpen(url, 'AlarmConfirm', "警告", 190, 400, true, 'AlarmConfirmIf');
    }
}

/*
   * 弹出询问窗口
   * @param alarmInfo          询问信息
   * @param parentFrameId      父frame的Id
   * @param callBackMethodName 回调函数名
*/
function AskConfirm(alarmInfo,parentFrameId,callBackMethodName){

    callBackMethodName=callBackMethodName.replace(new RegExp("&","gm"),"@");
    var url = encodeURI(encodeURI("commons/AskConfirm.jsp?parentFrameId="+parentFrameId+"&callBackMethodName="+callBackMethodName+"&alarmInfo="+alarmInfo));
    //alert(url);
    if(this.windowOpen){  //一定要加上this
    //if(windowOpen == undefined){
       windowOpen(url, 'AskConfirm', "询问", 190, 400, true, 'AskConfirmIf');
    }else{
       top.windowOpen(url, 'AskConfirm', "询问", 190, 400, true, 'AskConfirmIf');
    }
}

function common_submit(windowId,parentFrameId,callBackMethodName,t,h,w){
    if(windowId==null || windowId ==''){
		windowId='AskConfirm';
	}
    callBackMethodName=callBackMethodName.replace(new RegExp("&","gm"),"@");
    var url = encodeURI(encodeURI("commons/AskConfirm.jsp?parentFrameId="+parentFrameId+"&callBackMethodName="+callBackMethodName+"&alarmInfo="));
    //alert(url);
    if(this.windowOpen){  //一定要加上this
    //if(windowOpen == undefined){
       windowOpen(url, windowId, t, h, w, true, windowId);
    }else{
       top.windowOpen(url, windowId, t, h, w, true, windowId);
    }
}

/*
   * 弹出询问窗口,特殊情况,点击确定执行一个方法,点击取消执行另一个方法
   * @param alarmInfo                  询问信息
   * @param parentFrameId              父frame的Id
   * @param callBackOKMethodName       点击确定的回调函数名
   * @param callBackCancelMethodName   点击取消的回调函数名
*/
function AskConfirmTwo(alarmInfo,parentFrameId,callBackOKMethodName,callBackCancelMethodName){
    var url = encodeURI(encodeURI("commons/AskConfirmTwo.jsp?parentFrameId="+parentFrameId+"&callBackOKMethodName="+callBackOKMethodName+"&callBackCancelMethodName="+callBackCancelMethodName+"&alarmInfo="+alarmInfo));
    //alert(url);
    if(this.windowOpen){  //一定要加上this
    //if(windowOpen == undefined){
       windowOpen(url, 'AskConfirm', "询问", 190, 400, true, 'AskConfirmIf');
    }else{
       top.windowOpen(url, 'AskConfirm', "询问", 190, 400, true, 'AskConfirmIf');
    }
}
/***************后台计算时,要在页面显示一个转动的图片*********************/
	var loadingDiv = null;
	var loadingDiv2 = null;
	var frameObj = null;
	//显示DIV 覆盖页面主体
	function showLoading(message){
		initDiv(message);

		loadingDiv.appendChild(frameObj);
		document.body.appendChild(loadingDiv);
		document.body.appendChild(loadingDiv2);

	}
	//隐藏DIV  界面可以操作
	function hiddenLoading(){
		frameObj.removeNode(true);
		loadingDiv.removeNode(true);
		loadingDiv2.removeNode(true);
	}
	//初始化DIV变量
	function initDiv(message){
		if(loadingDiv == null){
			loadingDiv = document.createElement("div");
			loadingDiv.id = "loadingDiv";
			loadingDiv.style.zIndex = 100;
			loadingDiv.style.position = "absolute";
			loadingDiv.style.height = "100%";
			loadingDiv.style.width = "100%";
			loadingDiv.style.backgroundColor = "red";
			loadingDiv.style.filter = "alpha(opacity=80)";
			loadingDiv.style.top = "0px";
			loadingDiv.style.left = "0px";
		}

		if(loadingDiv2 == null){
		    loadingDiv2 = document.createElement("div");
			loadingDiv2.id="loadingDiv2";
			loadingDiv2.style.zIndex = 150;
			loadingDiv2.innerHTML = "<img style=\"position:relative;top:8px\" src='images/blue-loading.gif'/>"
			loadingDiv2.style.left = (document.body.scrollWidth - loadingDiv2.offsetWidth - document.body.scrollLeft -200)/2;
			loadingDiv2.style.top = (document.body.scrollHeight- loadingDiv2.offsetHeight - document.body.scrollTop - 200)/2;
			loadingDiv2.style.position = "absolute";
			loadingDiv2.style.width = "160px";
			loadingDiv2.style.height = "40px";
			var showMessage = (message != undefined || message != null) ? message : "Loading....";
			loadingDiv2.appendChild(document.createTextNode(showMessage));
		}
		if(frameObj == null){
			frameObj = document.createElement("iframe");
			frameObj.style.zIndex = -1;
			frameObj.style.position = "absolute";
			frameObj.width = "100%";
			frameObj.style.height = "100%";
			frameObj.style.top = "0";
			frameObj.style.left = "0";
			frameObj.border = 0;
		}
	}
/************表单提交*********************/
/**
 * 表单操作对象
 */
function WindowObject(_formId,buttonArray){
	var formId = _formId

	this.init = function(){
		var submitInput = document.createElement("input");
		submitInput.type = "submit";
		submitInput.id = "_mysubmit";
		submitInput.style.display = "none";
		$(formId).appendChild(submitInput);
	}
	/**
	 * 表单提交
	 */
	this.ajaxSubmit = function(url,param,callback){
		var flag = $(formId).fireEvent("onSubmit");
		if(!flag) return;

		_setButtonStatus(false); //设置按钮不可用
		sendAjaxRequest(url,param,_submitCallBack);
		//条件返回后的  回调
		function _submitCallBack(result){
			_setButtonStatus(true);//设置按钮可用
  			//异常检查
			ajaxSubmitException(result.responseText);
			callback(result);
		}
	}
	/**
	 * 表单提交
	 */
	this.formSumit = function(){
		var flag = $(formId).fireEvent("onsubmit");
		if(!flag) return;
		windowObj.setButtonStatus(false);
		$(formId)._mysubmit.click();
	}
	/**
	 * 保存并新增
	 */
	this.saveAndAdd = function(url,param,callBack){
		var flag = $(formId).fireEvent("onSubmit");
		if(!flag) return;

		_setButtonStatus(false); //设置按钮不可用
		sendAjaxRequest(url,param,_saveAndAddCallBack);
		//保存并新增后的回调
		function _saveAndAddCallBack(result){
			_setButtonStatus(true);//设置按钮可用
	  		//异常检查
			ajaxSubmitException(result.responseText);
			var flag = callBack(result);
			if(flag != false)
				window.location.reload();
		}
	}
	/**
	 * 关闭窗体,在关闭前,验证数据是否有修改
	 * @param closeFlag  窗体数据是否修改标识
	 * @param callBackName 不可以直接关闭,要求先保存时的保存方法
	 */
	this.winClose = function(closeFlag,saveMethodName,prentId){
		//窗体不可以直接关闭
		if(!closeFlag){
			AskConfirmTwo("数据有改变,是否保存修改的数据",prentId,saveMethodName,"windowClose");
		}else{//可以直接关闭,就直接关闭, 否则提示用户保存后直接关闭.
			windowClose();
		}
	}
	/**
	 *  直接关闭窗体
	 */
	this.zhijieClose = function(){
		windowClose();
	}
	/**
	 * 设置按钮状态
	 */
	function _setButtonStatus(flag){
		if(buttonArray == "undefined") return;
		if(typeof flag != "boolean") return;

		for(var i = 0; i < buttonArray.length; i++)
			document.getElementById(buttonArray[i]).disabled = !flag;
	}
	/**
	 * 设置按钮的状态,ture 可用.false 不可用
	 */
	this.setButtonStatus = function(flag){
		_setButtonStatus(flag);
	}
}

/**
 * 发送ajax请求，简单封装下
 * @param {Object} url  请求URL
 * @param {Object} parameters 请求参数
 * @param {Object} callBack  响应回调
 */
function sendAjaxRequest(url,parameters,callBack){
	 new Ajax.Request(url,
            {method: 'post', parameters: parameters, onComplete:callBack});
}